// firstPackage/pages/commissionModule/commissionTransferVoucher/commissionTransferVoucher.js
import { chooseAndUploadQiniuActicon } from "../../../../service/upload_service"
import { queryTransferCommissionDetail, pushTransferCommissionInfo } from "../../../../service/commission_service"

import debounce from "../../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {

    },
    detailId: null,
    isFill: 0,
    dataList: [
      // {
      //   money: "",
      //   voucher: "",
      // }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      detailId: options.id,
      isFill: options.isFill
    })
    this.queryTransferCommissionDetail();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //查询信息:
  queryTransferCommissionDetail() {
    wx.showLoading();
    queryTransferCommissionDetail(this.data.detailId).then(res => {
      wx.hideLoading();
      console.log("JJog:获取到的信息是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          formData: res.data,
        })
        if (res.data.records.length < 1) {
          let obj = {
            money: "",
            voucher: "",
            settlementId: this.data.detailId,
          }
          let arr = [];
          arr.push(obj);
          this.setData({
            dataList: arr
          })
        } else {
          this.setData({
            dataList: res.data.records
          })
        }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryTransferCommissionDetail", err);
    })
  },

  cellInputChange(e) {
    let detail = e.detail;
    let index = e.currentTarget.dataset.index;
    let item = 'dataList[' + index + '].money';
    this.setData({
      [item]: detail.value,
    })
    if (detail.value) {
      if (!this.checkNumber(detail.value)) {
        wx.showToast({
          title: '佣金只能为数字',
          icon: "none"
        })
        this.setData({
          [item]: "",
        })
      }
    }
  },

  //上传转账凭证
  pushVoucherImgAction(e) {
    if (this.data.isFill == 1) {
      return;
    }
    let index = e.currentTarget.dataset.index;
    chooseAndUploadQiniuActicon('voucher').then(res => {
      console.log("JJLog:图片上传成功:" + JSON.stringify(res));
      wx.showToast({
        title: '上传成功',
      })
      let item = `dataList[` + index + `].voucher`;
      this.setData({
        [item]: res.complateUrl
      })
    }).catch(err => {
      console.log("JJLog:图片上传失败:" + JSON.stringify(err));
      wx.showToast({
        title: '图片上传失败',
        icon: "none"
      })
    })
  },

  checkNumber(theObj) {
    var reg = /^[0-9]+.?[0-9]*$/;
    if (reg.test(theObj)) {
      return true;
    }
    return false;
  },

  //删除一个凭证
  cancelOnePersonAction(e) {
    let index = e.currentTarget.dataset.index;
    let arr = this.data.dataList;
    arr.splice(index, 1)
    this.setData({
      dataList: arr
    })
  },
  //新增凭证
  addNewPersonAction() {
    let arr = this.data.dataList || [];
    let obj = {
      money: "",
      voucher: "",
      settlementId: this.data.detailId,
    }
    if (arr.length < 1) {
      arr.push(obj);
      this.setData({
        dataList: arr
      })
      return;
    }
    if (arr && arr.length > 0) {
      let tempObj = arr[arr.length - 1];
      if (tempObj.money && tempObj.voucher) {
        arr.push(obj)
        this.setData({
          dataList: arr
        })
      } else {
        wx.showToast({
          title: '请先完成上一个凭证信息内容',
          icon: "none"
        })
      }

    }
  },

  //提交
  pushVoucherAction() {
    let data = this.data.dataList;
    console.log("JJLog:提交参数是:" + JSON.stringify(data));
    let totalMoney = 0;
    data.forEach((i) => {
      totalMoney = totalMoney + i.money
    })

    let isFill = true;
    data.forEach((t) => {
      if (!t.money || !t.voucher) {
        isFill = false
      }
    })
    if (!isFill) {
      wx.showToast({
        title: '请将凭证信息填写完整',
        icon: "none"
      })
      return;
    }

    if (totalMoney > this.data.formData.totalMoney) {
      wx.showModal({
        title: '提示',
        content: '结佣金额已超过总金额，是否继续提交',
        success: res => {
          if (res.confirm) {
            debounce(this.pushTransferCommissionInfo);
          }
        }
      })

    } else {
      debounce(this.pushTransferCommissionInfo);
    }

  },


  pushTransferCommissionInfo() {
    wx.showLoading();
    pushTransferCommissionInfo(this.data.dataList).then(res => {
      wx.hideLoading();
      console.log("JJog:获取到的信息是:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.showToast({
          title: '提交成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 2000)
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "提交失败",
        icon: "none"
      })
      console.error("JJLog:pushTransferCommissionInfo", err);
    })
  },

  callPhoneAction(e) {
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
      fail: err => {

      }
    })
  },

})